import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
import $http from "../util/https/index.js"

import createPersistedState from 'vuex-persistedstate'
const vuexPersisted = new createPersistedState({
	storage: {
		getItem: key => uni.getStorageSync(key),
		setItem: (key, value) => uni.setStorageSync(key, value),
		removeItem: key => uni.removeStorageSync(key)
	}
})

const store = new Vuex.Store({
	plugins: [vuexPersisted],
	state: {
		hasLogin: false,
		userInfo: "",
		token: "",
		appInfo: "",
		appMessage: {
			brand: null,
			pack: null,
			version: "1.0.0"
		},
		osVersion: null, //系统版本
		appVipSwitch: true,
		ad_switch: true,
		pushSwitch: false,
		adpid: "",
		oaid: null,
		imei: null,
		deviceId: null,
		pack: "", //上报pack
		propertyID: "", //上报软件id						
		fee_switch: false, //免费试用开关				
		incentive_ad_id: "",
		alipay_switch: true,
		wechat_pay_switch: true,

		tryOutStatus: true,
		tryOutNum: 2
	},
	getters: {
		hasLogin: (state) => {
			return state.hasLogin
		},
		userInfo: (state) => {
			return state.userInfo
		},
		token: (state) => {
			return state.token
		},

		appInfo: (state) => {
			return state.appInfo
		},
		appMessage: (state) => {
			return state.appMessage
		},
		osVersion: (state) => {
			return state.osVersion
		},
		appVipSwitch: (state) => {
			return state.appVipSwitch
		},
		ad_switch: (state) => {
			return state.ad_switch
		},
		pushSwitch: (state) => {
			return state.pushSwitch
		},
		adpid: (state) => {
			return state.adpid
		},

		oaid: (state) => {
			return state.oaid
		},
		imei: (state) => {
			return state.imei
		},
		deviceId: (state) => {
			return state.deviceId
		},
		pack: (state) => {
			return state.pack
		},
		propertyID: (state) => {
			return state.propertyID
		},
		get_imei_switch: (state) => {
			return state.get_imei_switch
		},
		alipay_switch: (state) => {
			return state.alipay_switch
		},
		wechat_pay_switch: (state) => {
			return state.wechat_pay_switch
		},

		tryOutStatus: (state) => {
			return state.tryOutStatus
		},
		tryOutNum: (state) => {
			return state.tryOutNum
		},
	},


	mutations: {
		updatelogin(state, data) {
			state.hasLogin = true;
		},
		updateToken(state, data) {
			uni.setStorageSync("token", JSON.stringify(data))
			state.token = data;
		},
		updateUserInfo(state, data) {
			state.userInfo = data;
		},
		logout(state) {
			state.hasLogin = false;
			state.token = '';
			state.userInfo = '';
			state.storyData = ""
		},
		updataAppInfo(state, data) {
			state.appInfo = data;
		},
		updataAppMessage(state, data) {
			state.appMessage = data;
		},
		updataAd_switch(state, data) {
			state.ad_switch = data;
		},
		updataPushSwitch(state, data) {
			state.pushSwitch = data;
		},
		updataOsVersion(state, data) {
			state.osVersion = data;
		},

		updataAppVipSwitch(state, data) {
			state.appVipSwitch = data;
		},
		updataAdpid(state, data) {
			state.adpid = data != '' ? parseInt(data) : "";
		},
		updataaddrIsCheck(state, data) {
			state.addrCheck = data || false;
		},
		updataOaid(state, data) {
			state.oaid = data;
		},
		updataImei(state, data) {
			state.imei = data;
		},
		updataDeviceId(state, data) {
			state.deviceId = data;
		},

		updataAdpid(state, data) {
			state.adpid = data != '' ? parseInt(data) : "";
		},

		updataOaid(state, data) {
			state.oaid = data;
		},
		updataImei(state, data) {
			uni.setStorageSync("imei", JSON.stringify(data))
			state.imei = data;
		},
		updataDeviceId(state, data) {
			state.deviceId = data;
		},
		update_get_imei_switch(state, data) {
			state.get_imei_switch = data
		},
		update_alipay_switch(state, data) {
			state.alipay_switch = data;
		},
		update_wechat_pay_switch(state, data) {
			state.wechat_pay_switch = data;
		},
		updataAdSwitch(state, data) {
			state.ad_switch = data;
		},
		updataTryOutStatus(state, data) {
			state.tryOutStatus = data;
		},
		updataTryOutNum(state, data) {
			console.log("updataTryOutNum", data)
			state.tryOutNum = data;
		},
	},
	actions: {
		commitGetAppInfo({
			commit
		}) {
			return new Promise((resolve, reject) => {
				console.log("commitGetAppInfo")
				$http.get('api/app/vipSwitch').then((res) => {
					console.log(res)
					commit('updataAdpid', res.data.ad_id);
					commit('update_alipay_switch', !!res.data.alipay_switch); // //支付宝支付开关					
					commit('update_wechat_pay_switch', !!res.data.wechat_pay_switch); // //微信支付开关																									

					let userSetAdSwitch = uni.getStorageSync("userSetAdSwitch")
					if (userSetAdSwitch) {
						console.log(JSON.parse(userSetAdSwitch))
						commit('updataAdSwitch', JSON.parse(userSetAdSwitch));
					} else {
						commit('updataAdSwitch', res.data.ad_switch);
					}






					resolve();
				})
			})
		},
		commitGetUserInfo({
			commit
		}) {
			return new Promise((resolve, reject) => {
				$http.post('/api/user/info').then((res) => {
					commit('updateUserInfo', res.data.info);
					resolve();
				})
			})
		},

	},


});
export default store;